<?php
/**
 * Handles display and validation of a form
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;

class Form {
	
	/**
	 * Fields associated with the form, in stdClass objects representing their database rows
	 *
	 * @var array
	 */
	protected $fields = array();
	
	/**
	 * The $id of the content this form is representing
	 *
	 * @var int
	 */
	protected $content;
	
	/**
	 * Success or error messages generated during processing
	 *
	 * @var array
	 */
	protected $messages = array();
	
	/**
	 * Did the form successfully create or modify an object?
	 * 
	 * @var bool
	 */
	public $success = FALSE;
	
	/**
	 * Initialize a form for content $content with fields $fields
	 *
	 * @param array $fields Array of stdClass objects representing the fields' database rows
	 * @param int $content The content ID
	 */
	public function __construct($fields, $content) {
		$this->content = $content;
		// Parse fields into something more usable
		foreach($fields as $field) {
			$this->fields[$field->cf_display_name] = $field;
		}
	}
	
	/**
	 * Go through each data type for this form's fields and run their formHeader() method
	 */
	public function header() {
		$ran = array();
		foreach($this->fields as $field) {
			if(!in_array($field->cf_data_type, $ran)) {
				$class = 'application\\datatypes\\' . $field->cf_data_type;
				$class::formHeader($field);
				$ran[] = $field->cf_data_type;
			}
			
		}
	}
	
	/**
	 * Validate the form
	 */
	public function validate() {
		
		
		if((int)method_exists($this->content, 'validateSpecificForm')) {
			$validated = $this->content->validateSpecificForm();
		}
		else {
			echo 'method does not exist';
		}
		
		if(!$validated) {
			$this->content->save($this->fields);
		}
	}
	
	/**
	 * Displays a form field on the form
	 *
	 * @param string $field	Display Name of the field you want to display
	 */
	public function displayField($field) {
		// Get the field from list of fields
		if($this->fields[$field]) {
			self::render($this->fields[$field], $this->content->data);
		}
	}
	
	/** 
	 * Render the form field according to the datatype's build and display methods
	 */
	protected function render($field) {
		$class = 'application\\datatypes\\' . $field->cf_data_type;
		$build = $class::build($field, $this->content->data);
		$class::display($build);
	}
	
	/**
	 * Display messages generated during processing
	 */
	public function displayMessages() {
		if(count($this->content->messages['success'])) {
			$this->success = TRUE;
			echo '<div class="messages">';
			foreach($this->content->messages['success'] as $message) {
				echo '<p class="alert-success">'.$message.'</p>';
			}
			echo '</div>';
		}
		if(count($this->content->messages['error'])) {
			echo '<div class="messages">';
			foreach($this->content->messages['error'] as $message) {
				echo '<p class="alert-error">'.$message.'</p>';
			}
			echo '</div>';
		}
		if(count($this->content->messages['alert'])) {
			echo '<div class="messages">';
			foreach($this->content->messages['alert'] as $message) {
				echo '<p class="alert-alert">'.$message.'</p>';
			}
			echo '</div>';
		}
	}
	
	/**
	 * Opening form tags and any hidden system fields
	 */
	public function openForm() {
		echo '<form method="post">';
		\application\system\Gandalf::tokenField();
		$this->displayField('ID');
	}
	
	/**
	 * Render the form according to the columns and order
	 *
	 * @deprecated Need to redo this, but it's working for now
	 */
	public function renderDefaultLayout() {
		if(count($this->content->fieldColumns[1])) : ?>
		<div class="form-one-column">
			<?php 
				foreach($this->content->fieldColumns[1] as $field) {
					$this->render($field);
				}
			?>
		</div>
		<?php endif;?>
		<?php if(count($this->content->fieldColumns[2]) || count($this->content->fieldColumns[3])) : ?>
		<div class="form-left-column">
			<?php 
				foreach($this->content->fieldColumns[2] as $field) {
					$this->render($field);
				}
			?>
		</div>
		<div class="form-right-column">
			<?php 
				foreach($this->content->fieldColumns[3] as $field) {
					$this->render($field);
				}
			?>
		</div>
		<div style="clear:both">
		<?php endif;?>
		<?php if(count($this->content->fieldColumns[4])) : ?>
		<div class="form-one-column">
			<?php 
				foreach($this->content->fieldColumns[4] as $field) {
					$this->render($field);
				}
			?>
		</div>
		<?php endif;
	}
	
	
}